/*
 * MainFrame.java
 */

package choreganizer;

import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import org.jdesktop.application.Task;
import org.jdesktop.application.Task.InputBlocker;

/**
 * The application's main frame.
 */
public class MainFrame extends FrameView {

    public MainFrame(SingleFrameApplication app) {
        super(app);

        initComponents();

        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {
	    @Override public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
            @Override public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
	    @Override public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String)(evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer)(evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });
    }

    
    
    
    @Action
    public void newAccount() {
	NewAccountDialog newAcc = new NewAccountDialog(getFrame(), true);
	newAcc.setLocationRelativeTo(getFrame());
	Startup.getApplication().show(newAcc);
    }
    
    @Action
    public void switchAccount() {
    }
    
    @Action
    public void importAccount() {
    }

    @Action
    public void exportAccount() {
    }

    @Action
    public void editSettings() {
    }
    
    @Action
    public void addChore() {
	// Example of how to make the progress bar move
	org.jdesktop.application.TaskService ts = 
	    Startup.getApplication().getContext().getTaskService();
	MyTask mt = new MyTask();
	// This line will trigger our glassPane blocker
	mt.setInputBlocker(new MyInputBlocker(mt));
	ts.execute(mt);
    }

    @Action(enabledProperty = "choreSelected")
    public void editChore() {
    }

    @Action(enabledProperty = "choreSelected")
    public void deleteChore() {
    }
    
    @Action
    public void showAboutBox() {
	JDialog aboutBox = new AboutDialog(getFrame());
	aboutBox.setLocationRelativeTo(getFrame());
        Startup.getApplication().show(aboutBox);
    }

    
    private boolean choreSelected = false;
    public boolean isChoreSelected() {
        return choreSelected;
    }

    public void setChoreSelected(boolean b) {
        boolean old = isChoreSelected();
        this.choreSelected = b;
        firePropertyChange("choreSelected", old, isChoreSelected());
    }
    
    
    class MyTask extends Task { 
	public MyTask() {
	    super(Startup.getApplication());
	}
	
	@Override 
	protected Object doInBackground() throws Exception, UnsupportedOperationException {
	    message("startMessage"); 
	    // do the work ... if an error is encountered:
	    System.out.println("Going");
	    Thread.currentThread().sleep(4000);
	    //message("errorMessage");
	    message("finishedMessage"); 
	    // .. return the result
	    return null;
	}
    }

    class MyInputBlocker extends InputBlocker {
	
	MyGlassPane busyPane = new MyGlassPane("Busy Test");
	
	public MyInputBlocker(Task task) {
	    super(task, Task.BlockingScope.WINDOW, 
		Startup.getApplication().getMainFrame().getGlassPane());
	}
	
	@Override
	protected void block() {
	    Startup.getApplication().getMainFrame().setGlassPane(busyPane);
	    busyPane.setVisible(true);
	}
	
	@Override
	protected void unblock() {
	    busyPane.setVisible(false);
	}
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        calendarTable1 = new choreganizer.calendar.CalendarTable();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        newAccountMenuItem = new javax.swing.JMenuItem();
        switchAcountMenuItem = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JSeparator();
        importAccount = new javax.swing.JMenuItem();
        exportAccount = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        configureMenu = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        choreMenu = new javax.swing.JMenu();
        addChoreMenuItem = new javax.swing.JMenuItem();
        editChoreMenuItem = new javax.swing.JMenuItem();
        removeChoreMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();

        mainPanel.setName("mainPanel"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        calendarTable1.setName("calendarTable1"); // NOI18N
        jScrollPane1.setViewportView(calendarTable1);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(choreganizer.Startup.class).getContext().getResourceMap(MainFrame.class);
        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jButton1.setFont(resourceMap.getFont("jButton1.font")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setBorderPainted(false);
        jButton1.setFocusable(false);
        jButton1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        jButton1.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jButton1.setName("jButton1"); // NOI18N

        jButton2.setFont(resourceMap.getFont("jButton2.font")); // NOI18N
        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setBorderPainted(false);
        jButton2.setFocusable(false);
        jButton2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jButton2.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jButton2.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jButton2.setName("jButton2"); // NOI18N

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 235, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 246, Short.MAX_VALUE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 609, Short.MAX_VALUE)
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 402, Short.MAX_VALUE))
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(choreganizer.Startup.class).getContext().getActionMap(MainFrame.class, this);
        newAccountMenuItem.setAction(actionMap.get("newAccount")); // NOI18N
        newAccountMenuItem.setName("newAccountMenuItem"); // NOI18N
        fileMenu.add(newAccountMenuItem);

        switchAcountMenuItem.setAction(actionMap.get("switchAccount")); // NOI18N
        switchAcountMenuItem.setName("switchAcountMenuItem"); // NOI18N
        fileMenu.add(switchAcountMenuItem);

        jSeparator2.setName("jSeparator2"); // NOI18N
        fileMenu.add(jSeparator2);

        importAccount.setAction(actionMap.get("importAccount")); // NOI18N
        importAccount.setName("importAccount"); // NOI18N
        fileMenu.add(importAccount);

        exportAccount.setAction(actionMap.get("exportAccount")); // NOI18N
        exportAccount.setName("exportAccount"); // NOI18N
        fileMenu.add(exportAccount);

        jSeparator1.setName("jSeparator1"); // NOI18N
        fileMenu.add(jSeparator1);

        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        configureMenu.setText(resourceMap.getString("configureMenu.text")); // NOI18N
        configureMenu.setName("configureMenu"); // NOI18N

        jMenuItem1.setAction(actionMap.get("editSettings")); // NOI18N
        jMenuItem1.setName("jMenuItem1"); // NOI18N
        configureMenu.add(jMenuItem1);

        menuBar.add(configureMenu);

        choreMenu.setText(resourceMap.getString("choreMenu.text")); // NOI18N
        choreMenu.setName("choreMenu"); // NOI18N

        addChoreMenuItem.setAction(actionMap.get("addChore")); // NOI18N
        addChoreMenuItem.setName("addChoreMenuItem"); // NOI18N
        choreMenu.add(addChoreMenuItem);

        editChoreMenuItem.setAction(actionMap.get("editChore")); // NOI18N
        editChoreMenuItem.setName("editChoreMenuItem"); // NOI18N
        choreMenu.add(editChoreMenuItem);

        removeChoreMenuItem.setAction(actionMap.get("deleteChore")); // NOI18N
        removeChoreMenuItem.setName("removeChoreMenuItem"); // NOI18N
        choreMenu.add(removeChoreMenuItem);

        menuBar.add(choreMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        progressBar.setName("progressBar"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 609, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 431, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusAnimationLabel)
                    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(statusMessageLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 8, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(3, 3, 3))
        );

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    public javax.swing.JMenuItem addChoreMenuItem;
    public choreganizer.calendar.CalendarTable calendarTable1;
    public javax.swing.JMenu choreMenu;
    public javax.swing.JMenu configureMenu;
    public javax.swing.JMenuItem editChoreMenuItem;
    public javax.swing.JMenuItem exportAccount;
    public javax.swing.JMenuItem importAccount;
    public javax.swing.JButton jButton1;
    public javax.swing.JButton jButton2;
    public javax.swing.JLabel jLabel1;
    public javax.swing.JMenuItem jMenuItem1;
    public javax.swing.JScrollPane jScrollPane1;
    public javax.swing.JSeparator jSeparator1;
    public javax.swing.JSeparator jSeparator2;
    public javax.swing.JPanel mainPanel;
    public javax.swing.JMenuBar menuBar;
    public javax.swing.JMenuItem newAccountMenuItem;
    public javax.swing.JProgressBar progressBar;
    public javax.swing.JMenuItem removeChoreMenuItem;
    public javax.swing.JLabel statusAnimationLabel;
    public javax.swing.JLabel statusMessageLabel;
    public javax.swing.JPanel statusPanel;
    public javax.swing.JMenuItem switchAcountMenuItem;
    // End of variables declaration//GEN-END:variables

    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;
}
